package cn.com.anypay.manager.service.impl;

import cn.com.anypay.manager.model.Article;
import cn.com.anypay.manager.mapper.ArticleMapper;
import cn.com.anypay.manager.service.ArticleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 文章服务实现类
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    private static final Logger log = LoggerFactory.getLogger(ArticleServiceImpl.class);

    @Resource
    private ArticleMapper articleMapper;

    @Override
    public ResponseEntity<List<Article>> getArticleList() {
        log.info("获取文章列表");
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus, "published").orderByDesc(Article::getCreateTime);
        List<Article> articles = articleMapper.selectList(queryWrapper);
        log.debug("查询到文章数量: {}", articles.size());
        return ResponseEntity.ok(articles);
    }

    @Override
    public ResponseEntity<Article> getArticleInfo(Long id) {
        log.info("获取文章详情, id={}", id);
        Article article = articleMapper.selectById(id);
        if (article == null) {
            log.warn("未找到id={}的文章", id);
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(article);
    }

    @Override
    public ResponseEntity<Article> createArticle(Article article) {
        log.info("创建文章: {}", article);
        article.setCreateTime(new Date());
        int result = articleMapper.insert(article);
        if (result > 0) {
            log.info("文章创建成功, id={}", article.getId());
            return ResponseEntity.ok(article);
        }
        log.error("文章创建失败: {}", article);
        return ResponseEntity.badRequest().build();
    }

    @Override
    public ResponseEntity<Article> updateArticle(Long id, Article article) {
        log.info("更新文章, id={}, article={}", id, article);
        article.setId(id);
        int result = articleMapper.updateById(article);
        if (result > 0) {
            log.info("文章更新成功, id={}", id);
            return ResponseEntity.ok(article);
        }
        log.warn("文章更新失败, id={}", id);
        return ResponseEntity.notFound().build();
    }

    @Override
    public ResponseEntity<String> downloadArticle(Long id) {
        log.info("下载文章, id={}", id);
        Article article = articleMapper.selectById(id);
        if (article == null) {
            log.warn("未找到id={}的文章，无法下载", id);
            return ResponseEntity.notFound().build();
        }
        
        // 构建Markdown格式的文章内容
        String content = "# " + article.getTitle() + "\n\n" +
                "作者: " + article.getAuthor() + "\n" +
                "创建时间: " + article.getCreateTime() + "\n\n" +
                article.getContent();
        log.debug("文章下载内容生成成功, id={}", id);
        return ResponseEntity.ok()
                .header("Content-Disposition", "attachment; filename=\"" + article.getTitle() + ".md\"")
                .body(content);
    }

    @Override
    public ResponseEntity<Void> removeArticle(Long id) {
        log.info("删除文章, id={}", id);
        int result = articleMapper.deleteById(id);
        if (result > 0) {
            log.info("文章删除成功, id={}", id);
            return ResponseEntity.ok().build();
        }
        log.warn("文章删除失败, id={}", id);
        return ResponseEntity.notFound().build();
    }
}
